package com.example.petadopt.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petadopt.R;
import com.example.petadopt.common.Result;
import com.example.petadopt.dao.AdoptDao;
import com.example.petadopt.dao.CollectDao;
import com.example.petadopt.entity.Adopt;
import com.example.petadopt.entity.Collect;
import com.example.petadopt.entity.User;
import com.example.petadopt.ui.activity.MyApplicationsActivity;
import com.example.petadopt.ui.activity.MyCollectionsActivity;
import com.example.petadopt.ui.adapter.AdoptApplicationAdapter;
import com.example.petadopt.utils.CurrentUserUtils;

import java.util.ArrayList;
import java.util.List;

public class ProfileFragment extends Fragment {

    private User currentUser;
    private TextView tvUsername;
    private TextView tvUserId;
    private TextView tvMemberDesc;
    private TextView tvAdoptCount;
    private TextView tvCollectCount;
    private RecyclerView rvAdoptApplications;
    private AdoptApplicationAdapter adoptApplicationAdapter;

    private LinearLayout llAdoptApplications;
    private LinearLayout llCollectedPets;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        initView(view);
        initListener();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }

    /**
     * 初始化视图
     */
    private void initView(View view) {
        tvUsername = view.findViewById(R.id.tv_username);
        tvUserId = view.findViewById(R.id.tv_user_id);
        tvMemberDesc = view.findViewById(R.id.tv_member_desc);
        tvAdoptCount = view.findViewById(R.id.tv_adopt_count);
        tvCollectCount = view.findViewById(R.id.tv_collect_count);
        rvAdoptApplications = view.findViewById(R.id.rv_adopt_applications);
        llAdoptApplications = view.findViewById(R.id.ll_adopt_applications);
        llCollectedPets = view.findViewById(R.id.ll_collected_pets);

        // 设置RecyclerView
        rvAdoptApplications.setLayoutManager(new LinearLayoutManager(getContext()));
        adoptApplicationAdapter = new AdoptApplicationAdapter(new ArrayList<>());
        rvAdoptApplications.setAdapter(adoptApplicationAdapter);
    }

    /**
     * 加载数据
     */
    private void loadData() {
        // 获取当前用户信息
        currentUser = CurrentUserUtils.getCurrentUser();
        updateUserInfo();

        // 获取领养记录
        loadAdoptData();

        // 获取收藏记录
        loadCollectData();
    }

    /**
     * 更新用户信息
     */
    private void updateUserInfo() {
        if (currentUser != null) {
            tvUsername.setText(currentUser.getUsername());
            tvUserId.setText("ID: " + currentUser.getId());
        }
    }

    /**
     * 加载领养记录
     */
    private void loadAdoptData() {
        if (currentUser == null) return;

        Result<List<Adopt>> adoptResult = AdoptDao.getAdoptsByUserId(currentUser.getId());
        if (adoptResult.isSuccess()) {
            List<Adopt> adoptList = adoptResult.getData();
            // 更新领养数量
            tvAdoptCount.setText(String.valueOf(adoptList.size()));

            // 更新会员描述
            tvMemberDesc.setText("累计帮助 " + adoptList.size() + " 只宠物找到新家");

            // 更新领养申请列表
            adoptApplicationAdapter.updateData(adoptList);
        } else {
            tvAdoptCount.setText("0");
            tvMemberDesc.setText("累计帮助 0 只宠物找到新家");
        }
    }

    /**
     * 加载收藏记录
     */
    private void loadCollectData() {
        if (currentUser == null) return;

        Result<List<Collect>> collectResult = CollectDao.getCollectsByUserId(currentUser.getId());
        if (collectResult.isSuccess()) {
            List<Collect> collectList = collectResult.getData();
            // 更新收藏数量
            tvCollectCount.setText(String.valueOf(collectList.size()));
        } else {
            tvCollectCount.setText("0");
        }
    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        llAdoptApplications.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MyApplicationsActivity.class);
                startActivity(intent);
            }
        });

        llCollectedPets.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MyCollectionsActivity.class);
                startActivity(intent);
            }
        });
    }
} 